package com.ng.common.util;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ForthinkUtils {

	public static String getStatusRedisKey(String uuid) {
		return String.format("FORTHINK_%s", uuid);
	}

	public static String getCardRedisKey(int cardId) {
		return String.format("FORTHINK_CARD_%s", cardId);
	}

	public static String md5(String password) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] hashBytes = md.digest(password.getBytes());
			StringBuilder hexString = new StringBuilder(2 * hashBytes.length);
			for (byte b : hashBytes) {
				String hex = Integer.toHexString(0xff & b);
				if (hex.length() == 1) {
					hexString.append('0');
				}
				hexString.append(hex);
			}
			return hexString.toString();
		} catch (NoSuchAlgorithmException e) {
		}
		return "";
	}

	public static String post(String url,JSONObject jsonObject) {
		RequestBody stringBody = RequestBody.Companion.create(JsonKit.toJson(jsonObject),
				MediaType.Companion.parse("application/json;charset=utf-8"));
		okhttp3.Request request = new okhttp3.Request.Builder().url(url).post(stringBody).build();
		OkHttpClient okHttpClient = new OkHttpClient();
		Response response;
		try {
			response = okHttpClient.newCall(request).execute();
			if (response.isSuccessful()) {
				return response.body().string();
			}
		} catch (IOException e) {
		}
		return "";
	}
	
	public static String post(String url,String authorization,Object jsonObject) {
		Headers headers = new Headers.Builder()
		        .add("Content-Type", "application/json") 
		        .add("Authorization", authorization) 
		        .build();
		RequestBody stringBody = RequestBody.Companion.create(JsonKit.toJson(jsonObject),
				MediaType.Companion.parse("application/json;charset=utf-8"));
		okhttp3.Request request = new okhttp3.Request.Builder().url(url).headers(headers).post(stringBody).build();
		OkHttpClient okHttpClient = new OkHttpClient();
		Response response;
		try {
			response = okHttpClient.newCall(request).execute();
			if (response.isSuccessful()) {
				return response.body().string();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
	

	public static void login() {
		String url = "http://forthink.xin:20080/EHCommon/admin/user/login";
		JSONObject param = new JSONObject();
		param.put("http_api_version", "6.4.0");
		param.put("username", "admin");
		param.put("password", md5("admin1234"));
		String s=post( url, param);
		if(!StrKit.isBlank(s)) {
			JSONObject jsonObject=JSONObject.parseObject(s);
			if(1==jsonObject.getInteger("type")) {
				JSONObject result=jsonObject.getJSONObject("result");
				if(result.containsKey("Authorization")) {
					String authorization=result.getString("Authorization");
					getTag(authorization);
				}
			}
		}
	}
	
	public static void getTag(String authorization) {
		String url = "http://forthink.xin:20080/EHCommon/device/deviceCard/getTag";
		JSONObject param = new JSONObject();
		param.put("page", 1);
		param.put("limit", 1000);
		String s=post( url,authorization, param);
		if(!StrKit.isBlank(s)) {
			System.out.println(s);
		}
	}

	
	
	public static void getPersonList(String authorization) {
		String url = "http://forthink.xin:20080/EHCommon/company/personSetting/getPersonList";
		JSONObject param = new JSONObject();
		param.put("page", 1);
		param.put("limit", 1000);
		String s=post( url,authorization, param);
		if(!StrKit.isBlank(s)) {
			System.out.println(s);
		}
	}

	public static void main(String[] args) {
		login();
	}

}
